package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;


    /**
     * 预约
     *
     * @param map
     * @return
     */
    @Override
    public Result submit(Map map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        // t_ordersetting   检查 预约日期是否存在
        Date date = DateUtils.parseString2Date(orderDate);
        //1.查找当天是否有可预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        // 检查 预约日期是否已满
        int number = orderSetting.getNumber();              //获取可预约人数
        int reservations = orderSetting.getReservations(); //获取已预约人数
        if (number <= reservations) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //t_member 获取手机号码属性和体检套餐id
        String telephone = (String) map.get("telephone");
        String setmealId = (String) map.get("setmealId");
        // 2.判断是否注册会员
        Member member = memberDao.findByTelephone(telephone);
        if (member == null) {
            member = new Member();
            //不是 注册会员,设置会员属性
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(DateUtils.parseString2Date(orderDate));
            memberDao.add(member);
        } else {
            //  是会员，判断是否重复预约（同一个人同一个时间预约了同一个套餐）， t_order
            Order order = new Order();
            order.setMemberId(member.getId()); //获取会员id
            order.setOrderDate(date);           //获取时间
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId"))); //获取套餐
            Order condition = orderDao.findByCondition(order);
            if (condition != null) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        //  添加预约信息   t_order
        Order order = new Order();

        order.setMemberId(member.getId());  //预约会员id
        order.setOrderDate(DateUtils.parseString2Date(orderDate));           //预约预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO); //到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));   //设置套餐
        orderDao.add(order); //必须要将自动增长的id查询出来 @SelectKey

        //修改预约数量
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    /**
     * 查看展示预约信息
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        try {
            Map map = orderDao.findById(id);
            //取出预约日期
            Date date = (Date) map.get("orderDate");
            //去除时分秒
            String orderDate = DateUtils.parseDate2String(date, "yyyy-MM-dd");
            map.put("orderDate",orderDate);
            return new Result(false,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    //查询每个套餐人数
    @Override
    public List<Map<String, Object>> findBySetmealCount() {

        return setmealDao.findBySetmealCount();
    }

}
